package com.ruoyi.email.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.email.entity.EmailEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.ruoyi.common.core.redis.RedisCache;

@Service
public class EmailService {
    @Value("${spring.mail.username}")
    private String username;

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    private RedisCache redisCache;


    public AjaxResult sendSimpleMail(EmailEntity emailEntity) {

        try {
            MimeMessage mailMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true);
            //设置寄件人
            helper.setFrom(username);
            //设置收件人
            helper.setTo(emailEntity.getSendTo());
            //设置邮件标题
            helper.setSubject(emailEntity.getTitle());
            //设置邮件内容
            helper.setText(emailEntity.getDetail(), true);
            //发送邮件
            mailSender.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return AjaxResult.success("邮件发送成功");
    }


}

